﻿using System;
using System . Collections . Generic;
using System . Linq;
using System . Text;
using System . Threading . Tasks;
using System . Xml . Linq;

namespace WenceyWang . RichMan4L . Maps . Buildings
{
	public abstract class Building : MapObject
	{

		public virtual string Name { get; set; }

		public virtual Area Position { get; protected set; }

		public BuildingState State { get; protected set; }

		public Players . Player Owner { get { return Position . Owner; } }

		public virtual int Grade { get; protected set; }

		public virtual BuildingType Type { get; }

		public abstract bool EasyToDestroy { get; }

		public abstract long MaintenanceFee { get; }


		public virtual void Upgrade ( )
		{

		}

		public virtual void Stay ( Players . Player player )
		{

		}

		public override void UpdateView ( )
		{
		}

		public override void StartDay ( Calendars . GameDate nextDate )
		{

		}

		protected Building ( ) : base ( )
		{


		}

		public virtual void Build ( Area position , Players . Player player )
		{

		}
		public static List<BuildingType> BuildingTypeList { get; set; } = new List<BuildingType> ( );

		public static void RegisBuildingType ( Type buildingType )
		{
			if ( buildingType == null )
			{
				throw new ArgumentNullException ( nameof ( buildingType ) );
			}
			if ( !typeof ( Building ) . IsAssignableFrom ( buildingType ) )
			{
				throw new ArgumentException ( $"{nameof ( buildingType )} should assignable from Building" );
			}
			BuildingAttribute attribute;
			if ( ( attribute = ( BuildingAttribute ) buildingType . GetCustomAttributes ( typeof ( BuildingAttribute ) , true ) . Single ( ) ) == null )
			{
				throw new ArgumentException ( $"{nameof ( buildingType )} should have atribute" );
			}

			BuildingTypeList . Add ( new BuildingType ( attribute , buildingType ) );

		}



		public static void Build ( Area position , BuildingType buildingType , Players . Player player )
		{

			Building building = ( Building ) Activator . CreateInstance ( buildingType . EntryPoint );

		}

	}
}
